package edu.java.homework.hw03.ipj06;

import java.util.Arrays;
import java.util.Scanner;

public class Exer03 {

    /**
     * tests faithfullness of the task
     * @param args
     */
    public static void main(String[] args) {
	int[][] array;
	int dimention = 0;

	Scanner input = new Scanner(System.in);

	System.out.println("Enter what element to search: ");
	int elem=input.nextInt();
	
	System.out.println("Enter the dimention of the matrix.");

	dimention = input.nextInt();
	System.out.printf("N:\t%d\n", dimention);
	
	
	if (dimention < 2) {
	    throw new IllegalArgumentException(
		    "Incorrect input of dimention of the matrix ");
	}
	array= new int[dimention][dimention];
	inputMatrix(array);
	System.out.println("The matrix: ");
	outputMatrix(array);
	System.out.println();
	
	for (int i=0;i<array.length;i++) {
	    if(searchElem(elem,array[i])){
		System.out.print("Row: " + (i+1) + "\n");
		System.out.println(Arrays.toString(array[i]));
	    }
	}
	
	input.close();
    }
    /**
     * search elemrnt in array
     */
    public static boolean searchElem(int elem, int[] array){
	// TODO: verify input parameter array
	boolean result=false;
	int iter=0;
	while(result==false && iter<array.length){
	    if(array[iter]==elem){
		result=true;
	    }
	    iter++;
	}
	return result;
    }
    

    /**
     * create and initialize a matrix
     */
    public static void inputMatrix(int[][]array) {
	// TODO: verify input parameter
	Scanner input = new Scanner(System.in);
	
	for(int i=0;i<array.length;i++){
	    for(int j=0;j<array.length;j++){
		System.out.printf("arr[%d][%d]= ",i,j);
		array[i][j]=input.nextInt();
		System.out.println();
	    }  
	}
	input.close();
    }
    
    /**
     * print a matrix
     */
    public static void outputMatrix(int[][]array){
	// TODO: verify input parameter
	for(int i=0;i<array.length;i++){
	    for(int j=0; j<array.length;j++){
		System.out.printf("%d\t",array[i][j]);
	    }
	    System.out.println();
	}
    }
	
}


